package com.cn.kehong.service;


import com.cn.kehong.api.IProductDianciRecordService;
import com.cn.kehong.conf.Config;
import com.cn.kehong.dao.ProductDianciRecordMapper;
import com.cn.kehong.dao.XunjianTaskMapper;
import com.cn.kehong.domain.ProductDianciRecord;
import com.cn.kehong.domain.ProductDianciRecordExample;
import com.cn.kehong.domain.XunjianTask;
import com.cn.kehong.domain.XunjianTaskExample;
import com.cn.kehong.utils.CommonUtil;
import com.cn.kehong.utils.Constants;
import com.cn.kehong.utils.QueryForm;
import com.cn.kehong.utils.TemplateExcelUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ProductDianciRecordServiceImpl implements IProductDianciRecordService {

    @Autowired
    private ProductDianciRecordMapper productDianciRecordMapper;
    @Autowired
    private Config config;
    @Autowired
    private XunjianTaskMapper xunjianTaskMapper;
    @Override
    public List<ProductDianciRecord> getProductDianciRecordList(QueryForm queryForm) {
        int count = productDianciRecordMapper.selectProductDianciRecordsCount(queryForm);
        if(count == 0){
            return Lists.newArrayList();
        }
        List<ProductDianciRecord> productDianciRecords = productDianciRecordMapper.selectProductDianciRecords(queryForm);
        productDianciRecords.get(0).setCount(count);
        return productDianciRecords;
    }

    @Override
    public String addProductDianciRecord(ProductDianciRecord productDianciRecord) throws Exception {
        String id = CommonUtil.generateRandomNum("dianrecord-");
        Map<String,String> dutyMap = CommonUtil.getOnDutyAndWhiteDay();
        productDianciRecord.setBanCi(dutyMap.get("whiteDay"));
        productDianciRecord.setDutyDay(dutyMap.get("dutyDay"));
        productDianciRecord.setId(id);
        int count = productDianciRecordMapper.insertSelective(productDianciRecord);
        if (count>0){
            XunjianTask xunjianTask = new XunjianTask();
            xunjianTask.setId(CommonUtil.generateRandomNum("xjtask-"));
            xunjianTask.setFactoryName(productDianciRecord.getFactoryName());
            xunjianTask.setLineType("1");
            xunjianTask.setWorkProcess("分级");
            xunjianTask.setTaskName("分级电除磁");
            xunjianTask.setFrequency("1次/批");
            //电除磁罐号
            xunjianTask.setAttr1(productDianciRecord.getDianciCode());
            //绝缘油液位
            xunjianTask.setAttr2(productDianciRecord.getOil());
             // 励磁电压
            xunjianTask.setAttr3(productDianciRecord.getDianYa());
            // 励磁电流
            xunjianTask.setAttr4(productDianciRecord.getDianLiu());
            // 油温
            xunjianTask.setAttr5(productDianciRecord.getYouWen());
            // 分级产能
            xunjianTask.setAttr6(productDianciRecord.getFenjiProduct());
            // 除磁排渣量
            xunjianTask.setAttr7(productDianciRecord.getPaiZa());
            // 排渣量占比
            xunjianTask.setAttr8(productDianciRecord.getPaiZaPercent());
            xunjianTask.setCreateTime(new Date());
            String banCi = productDianciRecord.getBanCi();
            xunjianTask.setBanCi(StringUtils.equals(banCi, "1") ? "A" : "B");
            count = xunjianTaskMapper.insertSelective(xunjianTask);
            if(count < 1){
                throw new Exception("插入巡检表异常！");
            }
        }
        return CommonUtil.outStr(count);
    }

    @Override
    public String updateProductDianciRecord(ProductDianciRecord productDianciRecord) {
        String id = productDianciRecord.getId();
        if(StringUtils.isEmpty(id)){
            return "主键不能为空";
        }
        ProductDianciRecordExample example = new ProductDianciRecordExample();
        example.createCriteria().andIdEqualTo(id);
        int count = productDianciRecordMapper.updateByExampleSelective(productDianciRecord,example);
        return CommonUtil.outStr(count);
    }

    @Override
    public String deleteProductDianciRecord(String id) {
        if(StringUtils.isEmpty(id)){
            return "主键不能为空";
        }
        ProductDianciRecord update = new ProductDianciRecord();
        update.setStatus(Constants.DEL_STATUS);
        ProductDianciRecordExample example = new ProductDianciRecordExample();
        example.createCriteria().andIdEqualTo(id);
        int count = productDianciRecordMapper.updateByExampleSelective(update,example);
        return CommonUtil.outStr(count);
    }

    @Override
    public List<ProductDianciRecord> getDianProductCode(QueryForm queryForm) throws Exception {
        return productDianciRecordMapper.getDianProductCode(queryForm);
    }


}
